package com.example.kx023;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RadioGroup group = findViewById(R.id.radiogroup);
        Button button1 = findViewById(R.id.button1);

        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton button = findViewById(i);
                CharSequence text = button.getText();
                System.out.println(text);
                Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();
            }
        });


        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0; i < group.getChildCount();i++){
                    RadioButton r = (RadioButton) group.getChildAt(i);
                    if (r.isChecked()) Toast.makeText(MainActivity.this, r.getText(), Toast.LENGTH_SHORT).show();

                }
            }
        });


    }
}